package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSON;
import com.turbo.boot.crawler.spider.worker.BaseTurboApiCrawler;
import com.xuxueli.crawler.rundata.RunData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * 国际债券
 *
 * @author wuyn
 * @since 2024/4/17
 */
@Slf4j
@Service("gjzqExchangeCrawler")
public class GjzqExchangeCrawler extends BaseTurboApiCrawler<GjzqExchangeCrawlerPageParser.PageVo> {

    @Override
    public String remoteCall(RunData runData) {

        String result = "{}";
        try {
            URL url = new URL(runData.getUrl());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept", "application/json");

            connection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
            connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
            connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setRequestProperty("Connection", "keep-alive");
            connection.setRequestProperty("Origin", "https://quote.fx678.com");
            connection.setRequestProperty("Pragma", "no-cache");
            connection.setRequestProperty("Referer", "https://quote.fx678.com/");
            connection.setRequestProperty("Sec-Fetch-Dest", "empty");
            connection.setRequestProperty("Sec-Fetch-Mode", "cors");
            connection.setRequestProperty("Sec-Fetch-Site", "cross-site");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36");
            connection.setRequestProperty("sec-ch-ua", "Google Chrome\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"");
            connection.setRequestProperty("sec-ch-ua-mobile", "?0");
            connection.setRequestProperty("sec-ch-ua-platform", "macOS");

            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    result = line;
                }
                reader.close();
            } else {
               log.warn("GET request not worked");
            }
            return result;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }


    @Override
    public void parse(String result) {
        List<GjzqExchangeCrawlerPageParser.PageVo> pageVos = JSON.parseArray(result, GjzqExchangeCrawlerPageParser.PageVo.class);
        for (GjzqExchangeCrawlerPageParser.PageVo pageVo : pageVos) {
            try {
                getPageParser().parse(pageVo);
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }


//    @Override
//    protected BaseTurboCrawlerPageParser<BalticExchangeCrawlerPageParser.PageVo> getPageParser() {
//        return new BalticExchangeCrawlerPageParser();
//    }


    public static void main(String[] args) throws Exception {

//       String res = httpHelper.get("https://api-q.fx678img.com/exchangeSymbol.php?exchName=GJZQ");


        GjzqExchangeCrawler balticExchangeCrawler = new GjzqExchangeCrawler();
        balticExchangeCrawler.crawl();
    }
}
